package com.ruoyi.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.ruoyi.system.domain.InvoiceRecords;
import com.ruoyi.system.service.impl.InvoiceRecordsServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {




    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private InvoiceRecordsServiceImpl invoiceRecordsService;

    public NoModelDataListener(InvoiceRecordsServiceImpl invoiceRecordsService){
        this.invoiceRecordsService = invoiceRecordsService;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData(cachedDataList);
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData(List<Map<Integer, String>> cachedDataList) {
        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<Integer,String> map:cachedDataList) {
                log.info(map.toString());
                InvoiceRecords invoiceRecords = new InvoiceRecords();
                invoiceRecords.setWarehouseCode(map.get(0));
                invoiceRecords.setWarehouseName(map.get(1));
                if(map.get(1).equals("崇明仓库")){
                 invoiceRecords.setCompany("崇明巴士");
                }else if(map.get(1).equals("金山仓库")||map.get(1).equals("金交仓库")){
                    invoiceRecords.setCompany("金山巴士");
                }else{
                    invoiceRecords.setCompany("营运公司");
                }
                invoiceRecords.setRepairBillCode(map.get(2));
                invoiceRecords.setBusCode(map.get(3));
                invoiceRecords.setProductCode(map.get(4));
                invoiceRecords.setProductName(map.get(5));
                invoiceRecords.setModel(map.get(6));
                invoiceRecords.setSpecs(map.get(7));
                invoiceRecords.setProductPrice(map.get(8));
                invoiceRecords.setPlarnPrice(map.get(9));
                invoiceRecords.setProductCount(map.get(10));
                invoiceRecords.setProductMoney(map.get(11));
                invoiceRecords.setCreateTime(map.get(12));
                invoiceRecords.setProductBrand(map.get(13));
                invoiceRecords.setProvide(map.get(15));
                invoiceRecords.setSettlementMode("压库");
                invoiceRecordsService.insertInvoiceRecords(invoiceRecords);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("存储数据库成功！");
    }
}